 class Node{
    private Object val;
    private Node next;
    public Node(){

    }
    public Node(Object val){
        this.val=val;
    }
    public Node(Object val,Node next){
        this.val=val;
        this.next=next;
    }

    public Object getVal() {
        return val;
    }

    public void setVal(Object val) {
        this.val = val;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }
}
public class myLinkedList {
    Node head=new Node();
    int size=0;
    //链尾添加
    public void addData(Object value){
        this.size++;
        Node data=new Node(value);
        if(this.head==null){
            this.head=data;
            return;
        }
        Node cur=this.head;
        while(cur.getNext()!=null){
            cur=cur.getNext();
        }
        cur.setNext(data);
    }

    //链头添加
    public void addFirst(Object value){
        Node data=new Node(value);
        this.size++;
        if(this.head==null){
            this.head=data;
            return;
        }
        data.setNext(this.head);
        this.head=data;
    }

    //在指定位置插入
    public void insertData(int index, Object value){
        if(index<0||index>this.size){
            System.out.println("下标位置越界");
            return ;
        }
        if(index==0){
            this.addFirst(value);
            return ;
        }
        if(index==this.size){
            this.addData(value);
            return ;
        }
        Node cur=this.head;
        for(int i=0;i<index;i++){
            cur=cur.getNext();
        }
        Node data=new Node(value);
        data.setNext(cur.getNext());
        cur.setNext(data);
        this.size++;
    }

    //删除指定位置的节点
    public void deleteData(int index){
        if(index<0||index>this.size) {
            System.out.println("下标位置越界");
            return;
        }
        this.size--;
        if(index==0){
            if(this.head.getNext()!=null){
                this.head=this.head.getNext();
            }else{
                this.head=null;
            }
            return;
        }
        Node cur=this.head;
        for(int i=0;i<index-1;i++){
            cur=cur.getNext();
        }
        cur.setNext(cur.getNext().getNext());
    }
}